﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Manage.CryptUtil
{
    public static class Crypt
    {
        public static string Base64EnCrypt(string content, Encoding encoding = null)
        {
            encoding = encoding ?? Encoding.UTF8;
            return Convert.ToBase64String(encoding.GetBytes(content));
        }
        public static string Base64DeCrypt(string content, Encoding encoding = null)
        {
            encoding = encoding ?? Encoding.UTF8;
            return encoding.GetString(Convert.FromBase64String(content));
        }
        /// <summary>
        /// 3DES加密
        /// </summary>
        /// <param name="content">原文</param>
        /// <param name="key">密钥，长度24个字符</param>
        /// <param name="encoding">编码，默认UTF8</param>
        /// <returns></returns>
        public static byte[] Encrypt3DES(string content, string key, Encoding encoding = null)
        {
            if (string.IsNullOrEmpty(content))
                throw new Exception("原文不能为空！");
            if (string.IsNullOrEmpty(key))
                throw new Exception("密钥不能为空！");
            if (key.Length < 24)
                throw new Exception("密钥长度不足！");
            encoding = encoding ?? Encoding.UTF8;
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            //密钥
            des.Key = encoding.GetBytes(key.Substring(0, 24));
            //ECB模式
            des.Mode = CipherMode.ECB;
            //末尾填充方式为0
            des.Padding = PaddingMode.Zeros;
            ICryptoTransform DESEncrypt = des.CreateEncryptor();
            byte[] buffer = encoding.GetBytes(content);
            return DESEncrypt.TransformFinalBlock(buffer, 0, buffer.Length);
        }
        /// <summary>
        /// 3DES解密
        /// </summary>
        /// <param name="content">密文</param>
        /// <param name="key">密钥，长度24个字符</param>
        /// <param name="encoding">编码，默认UTF8</param>
        /// <returns></returns>
        public static string Decrypt3DES(byte[] content, string key, Encoding encoding = null)
        {
            if (null == content || content.Length < 1)
                throw new Exception("密文不能为空！");
            if (string.IsNullOrEmpty(key))
                throw new Exception("密钥不能为空！");
            if (key.Length < 24)
                throw new Exception("密钥长度不足！");
            encoding = encoding ?? Encoding.UTF8;
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            //密钥截取24个字符
            des.Key = encoding.GetBytes(key.Substring(0, 24));
            //ECB方式
            des.Mode = CipherMode.ECB;
            //末尾填充为0填充
            des.Padding = PaddingMode.Zeros;
            ICryptoTransform DESDecrypt = des.CreateDecryptor();
            string result = "";
            try
            {
                result = encoding.GetString(DESDecrypt.TransformFinalBlock(content, 0, content.Length));
            }
            catch (Exception)
            {
                throw;
            }
            return result;

        }

        /// <summary>
        /// 32位MD5加密
        /// </summary>
        /// <param name="txt">原文</param>
        /// <param name="sEncode">编码</param>
        /// <returns></returns>
        public static string MD32Crypt(string txt, Encoding sEncode = null)
        {
            try
            {
                sEncode = null == sEncode ? Encoding.UTF8 : sEncode;
                byte[] orgByte = sEncode.GetBytes(txt);
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] toByte = md5.ComputeHash(orgByte);
                string retPwd = "";
                for (int i = 0; i < toByte.Length; i++)
                {
                    retPwd += toByte[i].ToString("x2");
                }
                return retPwd;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 带盐32位MD5加密
        /// </summary>
        /// <param name="txt">原文</param>
        /// <param name="sault">盐</param>
        /// <param name="sEncode">编码</param>
        /// <returns></returns>
        public static string MD32Crypt(string txt, string sault, Encoding sEncode = null)
        {
            try
            {
                string s = MD32Crypt(txt, sEncode) + sault;
                s = MD32Crypt(s);
                return s;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

